<%-- 
    Document   : MainPageDescarga
    Created on : 25-may-2012, 3:46:25
    Author     : barre
--%>

<%@page import="java.util.Iterator"%>
<%@page import="java.net.URL"%>
<%@page import="login.ArticuloForm"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.io.InputStream"%>
<%@page import="java.io.IOException"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.File"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="refresh" content="1;url=MainPage.jsp">  
        <title>JSP Page</title>
    </head>
    <body>
        <%
            HashMap<ArticuloForm,Integer> descargasArts = (HashMap<ArticuloForm,Integer>) session.getAttribute("downloads");
            if(descargasArts == null) descargasArts = new HashMap<ArticuloForm, Integer>();
            
            //File f = new File("/home/barre/Workspaces/NetBeansProjects/SdCLoginJSP/src/java/files/" + request.getParameter("file"));
            ArticuloForm art = (ArticuloForm) session.getAttribute("fileArt");

            String fileSep = "/"; //System.getProperty("file.separator"); 
            String p;
            URL res = getClass().getClassLoader().getResource("files/articulos");
            p = res.toString();
            p = p.substring(0, p.lastIndexOf(fileSep));
            p = p.substring(0, p.lastIndexOf(fileSep));
            p = p.substring(0, p.lastIndexOf(fileSep));
            p = p.substring(0, p.lastIndexOf(fileSep));
            p = p.substring(0, p.lastIndexOf(fileSep));
            p = p.substring(0, p.lastIndexOf(fileSep));
            if(p.startsWith("/")){
                p = p.substring(p.indexOf(fileSep)+1, p.length());
            }else{
                p = p.substring(p.indexOf(fileSep), p.length());
            }
            p = p.replaceAll("%20"," ");
            p = p+fileSep+"src"+fileSep+"java"+fileSep+"files";
            
            File f = new File (p + fileSep + art.getArchivo());
            response.setContentType ("application/txt");
            //set the header and also the Name by which user will be prompted to save
            
            //Antes de filename podemos poner "inline" para mostrar el contenido online
            //o bien "attachment" para abrir un cuadro de dialogo de abrir/descargar
            response.setHeader( "Content-Disposition", "attachment; filename=" +request.getParameter("file"));
            //get the file name
            String name = f.getName().substring(f.getName().lastIndexOf(fileSep) + 1,f.getName().length());
            //OPen an input stream to the file and post the file contents thru the servlet output stream to the client m/c

            InputStream in = new FileInputStream(f);
            ServletOutputStream outs = response.getOutputStream();

            try {
                    int bit = in.read();
                    while ((bit) >= 0)
                    {
                        outs.write(bit);
                        bit = in.read();
                    }
                    //System.out.println("" +bit);
            } catch (IOException ioe) {
                    ioe.printStackTrace(System.out);
            }
            outs.flush();
            outs.close();
            in.close();

            boolean found = false;
            Iterator it = descargasArts.keySet().iterator();
            ArticuloForm af;
            while(it.hasNext()){
                    af = (ArticuloForm) it.next();
                    if(af.getTitulo().equals(art.getTitulo())&&(af.getArchivo().equals(art.getArchivo()))){
                        descargasArts.put(af, descargasArts.get(af)+1);
                        found = true;
                    }
            }

            if(!found){
                descargasArts.put(art, 1);
            }

                                      
            //if(!descargasArts.containsKey(art)) descargasArts.put(art, 1);
            //else descargasArts.put(art, descargasArts.get(art)+1);
            session.setAttribute("downloads", descargasArts);
%>
<jsp:forward page="MainPage.jsp" />
    </body>
</html>
